/**
 * Created by lvlq on 16/2/27.
 */
(function ($) {
    var L = function () {
        var A = window.document.documentElement.getBoundingClientRect();
        var raio = 1520.5 / 960;
        var width = A.width;
        var logicHeight = width * raio;
        if (logicHeight > A.height) {
            var kraio = A.height / logicHeight;
            var K = window.document.getElementById("container");
            K.style.fontSize = kraio + "rem";
            K.style.width = 10 * kraio + "rem";
        }
    };
    var K;
    window.addEventListener("resize", function () {
        clearTimeout(K), K = setTimeout(L, 300);
    });
    L();

    $("#back").on("tap", function () {
        window.history.back();
    });

    var clickFlag = false;
    $("#rolutte-btn").on("tap", function () {
        if (clickFlag) return;
        clickFlag = true;
        _smq.push(['custom', '活动2转盘页', '颜值转盘点击抽奖', '点击']);
        getPrize(function (data) {
            if (data.errno) {
                alert("网络错误");
                clickFlag = false;
            } else {
                user_id = data.data.id;
                rollTo(data.data, function () {
                    clickFlag = false;
                });
            }
        });
    });

    var rollTo = function (data, cb) {
        var id = data.pid;
        var pname = data.pname;
        var prize_roll = {
            "4": 0,
            "9": 60,
            "8": 120,
            "7": 180,
            "6": 240,
            "5": 300
        };

        $("#pan").animate({

            'transform': 'rotate(' + 10 * 360 + prize_roll[id] + 'deg)',
            '-webkit-transform': 'rotate(' + 10 * 360 + prize_roll[id] + 'deg)'
        }, 3e3, "linear", function () {
            $("#pan").css({
                'transform': 'rotate(' + prize_roll[id] + 'deg)',
                '-webkit-transform': 'rotate(' + prize_roll[id] + 'deg)'
            });
            if (id != 4) {
                $(".dialog").show();
                alert("恭喜您获得了" + pname + "~~");
            }

            cb && cb();
        });
    };

    $(".dialog-commit").on("tap", function () {
        var dto = {
            id: user_id,
            name: $(".input-name .input input").val(),
            phone: $(".input-phone .input input").val(),
            address: $(".input-address .input textarea").val()
        };


        if (!dto.name) {
            return alert("名字不能为空");
        }

        if (!dto.phone) {
            return alert("手机号不能为空");
        }

        if (!(/^1[3|4|5|8\7]\d{9}$/.test(dto.phone))) {
            return alert("手机号格式不正确");
        }

        if (!dto.address) {
            return alert("地址不能为空");
        }

        $.ajax({
            url: "/index/shell/update/address",
            data: dto,
            type: "POST",
            dataType: "json",
            success: function (data) {
                if (data.errno) {
                    alert("网络错误");
                } else {
                    window.location.href = "/index/shell/fight/share";
                }
            },
            error: function () {
                alert("网络错误");
            }
        })
    });
})(Zepto);

var getPrize = function (cb) {
    $.ajax({
        url: "/index/shell/prize",
        type: "POST",
        data: {
            aid: 2
        },
        dataType: "json",
        success: function (data) {
            cb && cb(data);
        },
        error: function () {
            alert("网络错误");
        }
    })
};